package com.housediyadmin.service
{
	import com.housediyadmin.events.salesEvents.GetSalesEvent;
	import com.housediyadmin.vo.Sales;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/12/16
	 */	
	public class SalesRemoteObjectService extends Actor
	{
		public var salesRemoteObject:RemoteObject;
		
		public function SalesRemoteObjectService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			salesRemoteObject = new RemoteObject("fluorine");
			salesRemoteObject.source = "HouseDiyService.HouseDiyAction";
			salesRemoteObject.showBusyCursor = true;
			salesRemoteObject.channelSet = channelSet;
			
			/*salesRemoteObject.getOperation("getAllSales").addEventListener(ResultEvent.RESULT, getAllSalesResult);
			salesRemoteObject.getOperation("getAllSales").addEventListener(FaultEvent.FAULT, faultHandler);
			
			salesRemoteObject.getOperation("deleteSales").addEventListener(ResultEvent.RESULT, deleteSalesResult);
			salesRemoteObject.getOperation("deleteSales").addEventListener(FaultEvent.FAULT, faultHandler);
			
			salesRemoteObject.getOperation("addSales").addEventListener(ResultEvent.RESULT, addSalesResult);
			salesRemoteObject.getOperation("addSales").addEventListener(FaultEvent.FAULT, faultHandler);
			
			salesRemoteObject.getOperation("editSales").addEventListener(ResultEvent.RESULT, editSalesResult);
			salesRemoteObject.getOperation("addSales").addEventListener(FaultEvent.FAULT, faultHandler);*/
		}
		
		public function getSalesList():void
		{
			salesRemoteObject.getAllSales();
		}
		
		public function deleteSalesByIds(salesIds:Array):void
		{
		    salesRemoteObject.deleteSales(salesIds);	
		}
		
		public function addOneSales(salse:Sales, goodsIds:Array = null):void
		{
			salesRemoteObject.addSales(salse, goodsIds);
		}
		
		public function editOneSales(sales:Sales, goodsIds:Array = null):void
		{
			salesRemoteObject.editSales(sales, goodsIds);
		}
		
		private function getAllSalesResult(event:ResultEvent):void
		{
			dispatch(new GetSalesEvent(GetSalesEvent.UPDATE_SALES_MODEL, event.result as ArrayCollection));
		}
		
		private function deleteSalesResult(event:ResultEvent):void
		{
			dispatch(new GetSalesEvent(GetSalesEvent.UPDATE_SALES_MODEL, event.result as ArrayCollection));
		}
		
		private function addSalesResult(event:ResultEvent):void
		{
			dispatch(new GetSalesEvent(GetSalesEvent.UPDATE_SALES_MODEL, event.result as ArrayCollection));
		}
		
		private function editSalesResult(event:ResultEvent):void
		{
			dispatch(new GetSalesEvent(GetSalesEvent.UPDATE_SALES_MODEL, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}